{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-05T12:01:42.447338Z",
     "start_time": "2023-07-05T12:01:42.337107Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "from sklearn.preprocessing import PolynomialFeatures\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import mean_squared_error\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-05T12:01:42.461284Z",
     "start_time": "2023-07-05T12:01:42.448051Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(397, 9)\n"
     ]
    },
    {
     "data": {
      "text/plain": "    mpg  cylinders  displacement horsepower  weight  acceleration  year  \\\n0  18.0          8         307.0        130    3504          12.0    70   \n1  15.0          8         350.0        165    3693          11.5    70   \n2  18.0          8         318.0        150    3436          11.0    70   \n3  16.0          8         304.0        150    3433          12.0    70   \n4  17.0          8         302.0        140    3449          10.5    70   \n\n   origin                       name  \n0       1  chevrolet chevelle malibu  \n1       1          buick skylark 320  \n2       1         plymouth satellite  \n3       1              amc rebel sst  \n4       1                ford torino  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>mpg</th>\n      <th>cylinders</th>\n      <th>displacement</th>\n      <th>horsepower</th>\n      <th>weight</th>\n      <th>acceleration</th>\n      <th>year</th>\n      <th>origin</th>\n      <th>name</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>18.0</td>\n      <td>8</td>\n      <td>307.0</td>\n      <td>130</td>\n      <td>3504</td>\n      <td>12.0</td>\n      <td>70</td>\n      <td>1</td>\n      <td>chevrolet chevelle malibu</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>15.0</td>\n      <td>8</td>\n      <td>350.0</td>\n      <td>165</td>\n      <td>3693</td>\n      <td>11.5</td>\n      <td>70</td>\n      <td>1</td>\n      <td>buick skylark 320</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>18.0</td>\n      <td>8</td>\n      <td>318.0</td>\n      <td>150</td>\n      <td>3436</td>\n      <td>11.0</td>\n      <td>70</td>\n      <td>1</td>\n      <td>plymouth satellite</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>16.0</td>\n      <td>8</td>\n      <td>304.0</td>\n      <td>150</td>\n      <td>3433</td>\n      <td>12.0</td>\n      <td>70</td>\n      <td>1</td>\n      <td>amc rebel sst</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>17.0</td>\n      <td>8</td>\n      <td>302.0</td>\n      <td>140</td>\n      <td>3449</td>\n      <td>10.5</td>\n      <td>70</td>\n      <td>1</td>\n      <td>ford torino</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv(r'../data/Auto.csv')\n",
    "print(data.shape)\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-05T12:01:42.466516Z",
     "start_time": "2023-07-05T12:01:42.463665Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "mpg             float64\ncylinders         int64\ndisplacement    float64\nhorsepower       object\nweight            int64\nacceleration    float64\nyear              int64\norigin            int64\nname             object\ndtype: object"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-05T12:01:42.513538Z",
     "start_time": "2023-07-05T12:01:42.471779Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array(['130', '165', '150', '140', '198', '220', '215', '225', '190',\n       '170', '160', '95', '97', '85', '88', '46', '87', '90', '113',\n       '200', '210', '193', '?', '100', '105', '175', '153', '180', '110',\n       '72', '86', '70', '76', '65', '69', '60', '80', '54', '208', '155',\n       '112', '92', '145', '137', '158', '167', '94', '107', '230', '49',\n       '75', '91', '122', '67', '83', '78', '52', '61', '93', '148',\n       '129', '96', '71', '98', '115', '53', '81', '79', '120', '152',\n       '102', '108', '68', '58', '149', '89', '63', '48', '66', '139',\n       '103', '125', '133', '138', '135', '142', '77', '62', '132', '84',\n       '64', '74', '116', '82'], dtype=object)"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['horsepower'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-05T12:01:42.513878Z",
     "start_time": "2023-07-05T12:01:42.475643Z"
    }
   },
   "outputs": [],
   "source": [
    "data['horsepower'] = data['horsepower'].replace('?',np.nan)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-05T12:01:42.514201Z",
     "start_time": "2023-07-05T12:01:42.479777Z"
    }
   },
   "outputs": [],
   "source": [
    "data = data.dropna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-05T12:01:42.531846Z",
     "start_time": "2023-07-05T12:01:42.484049Z"
    }
   },
   "outputs": [],
   "source": [
    "data['horsepower'] = data['horsepower'].astype('int')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-05T12:01:42.591190Z",
     "start_time": "2023-07-05T12:01:42.488233Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "mpg             float64\ncylinders         int64\ndisplacement    float64\nhorsepower        int64\nweight            int64\nacceleration    float64\nyear              int64\norigin            int64\nname             object\ndtype: object"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-05T12:01:42.591346Z",
     "start_time": "2023-07-05T12:01:42.491368Z"
    }
   },
   "outputs": [],
   "source": [
    "powers = np.arange(1,11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-05T12:01:42.630658Z",
     "start_time": "2023-07-05T12:01:42.504195Z"
    }
   },
   "outputs": [],
   "source": [
    "errors = []\n",
    "for power in powers:\n",
    "    poly = PolynomialFeatures(power)\n",
    "    X = poly.fit_transform(data['horsepower'].to_frame())\n",
    "    y = data['mpg']\n",
    "    X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.1,random_state = 0)\n",
    "\n",
    "    lr = LinearRegression()\n",
    "    lr.fit(X_train,y_train)\n",
    "    errors.append(mean_squared_error(y_test,lr.predict(X_test)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-07-05T12:01:42.879388Z",
     "start_time": "2023-07-05T12:01:42.532042Z"
    }
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "__init__() got an unexpected keyword argument 'size'",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "Cell \u001B[0;32mIn[11], line 3\u001B[0m\n\u001B[1;32m      1\u001B[0m temp \u001B[38;5;241m=\u001B[39m pd\u001B[38;5;241m.\u001B[39mDataFrame({\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mpower\u001B[39m\u001B[38;5;124m\"\u001B[39m:powers,\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mErrors\u001B[39m\u001B[38;5;124m'\u001B[39m:errors})\n\u001B[1;32m      2\u001B[0m plt\u001B[38;5;241m.\u001B[39mfigure(figsize \u001B[38;5;241m=\u001B[39m (\u001B[38;5;241m12\u001B[39m,\u001B[38;5;241m6\u001B[39m))\n\u001B[0;32m----> 3\u001B[0m g \u001B[38;5;241m=\u001B[39m \u001B[43msns\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mFacetGrid\u001B[49m\u001B[43m(\u001B[49m\u001B[43mdata\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43m \u001B[49m\u001B[43mtemp\u001B[49m\u001B[43m,\u001B[49m\u001B[43msize\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;241;43m5\u001B[39;49m\u001B[43m)\u001B[49m\n\u001B[1;32m      4\u001B[0m g\u001B[38;5;241m.\u001B[39mmap(plt\u001B[38;5;241m.\u001B[39mscatter, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mpower\u001B[39m\u001B[38;5;124m'\u001B[39m , \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mErrors\u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[1;32m      5\u001B[0m g\u001B[38;5;241m.\u001B[39mmap(plt\u001B[38;5;241m.\u001B[39mplot, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mpower\u001B[39m\u001B[38;5;124m'\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mErrors\u001B[39m\u001B[38;5;124m'\u001B[39m)\n",
      "\u001B[0;31mTypeError\u001B[0m: __init__() got an unexpected keyword argument 'size'"
     ]
    },
    {
     "data": {
      "text/plain": "<Figure size 1200x600 with 0 Axes>"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "temp = pd.DataFrame({\"power\":powers,'Errors':errors})\n",
    "plt.figure(figsize = (12,6))\n",
    "g = sns.FacetGrid(data = temp,size=5)\n",
    "g.map(plt.scatter, 'power' , 'Errors')\n",
    "g.map(plt.plot, 'power', 'Errors')\n",
    "plt.title('LOOCV')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "errors_list = []\n",
    "from sklearn.utils import shuffle\n",
    "for i in range(10):\n",
    "    errors = []\n",
    "    n = len(data)\n",
    "    for power in powers:\n",
    "        poly = PolynomialFeatures(power)\n",
    "        X = poly.fit_transform(data['horsepower'].to_frame())\n",
    "        y = data['mpg']\n",
    "        X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.1,random_state = i)\n",
    "\n",
    "        lr = LinearRegression()\n",
    "        lr.fit(X_train,y_train)\n",
    "        errors.append(mean_squared_error(y_test,lr.predict(X_test)))\n",
    "    errors_list.append(errors)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "powers = np.arange(1,11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_dict = {'Errors'+ str(i):errors_list[i] for i in range(10)}\n",
    "temp = pd.DataFrame(data_dict)\n",
    "temp.index = powers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "temp.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "temp.plot.line(figsize = (12,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
